package com.promfy.draw;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.promfy.util.L;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;

public class CusDrawView extends View {
	private static final String TAG = "navi";

	protected static final int MSG_WHAT_INVAIDATE = 0;

	/**
	 * state
	 * 
	 * @author promfy
	 * 
	 */
	enum State {
		FLOATING, TOUCH_DOWN, LONG_PRESSED
	}

	private State state = State.FLOATING;

	/**
	 * constant var
	 */
	public static int CENTER_LENGTH = 200;
	public static int OUTTER_RIDUS_STROKE = 100;
	public static int INNER_RIDUS_STROKE = 80;
	public static int DEGREE_PADDING = 5;

	/**
	 * draw
	 */
	private Paint p;
	private Path path = new Path();
	private Path path2 = new Path();
	private Path path3 = new Path();
	private Path path4 = new Path();

	/**
	 * timer
	 */
	Timer timer;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case MSG_WHAT_INVAIDATE:
				invalidate();
				break;
			}
		};
	};

	public CusDrawView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		p = new Paint();
		p.setColor(Color.BLUE);
		p.setAlpha(100);
		p.setAntiAlias(true);
		canvas.drawCircle(CENTER_LENGTH, CENTER_LENGTH,
				INNER_RIDUS_STROKE - 10, p);

		// p.setStyle(Paint.Style.STROKE);
		// p.setStrokeWidth(2);
		L.v(TAG, "onDraw() state: " + state);

		switch (state) {
		case FLOATING:
			break;
		case LONG_PRESSED:
			canvas.drawCircle(CENTER_LENGTH, CENTER_LENGTH, CENTER_LENGTH - 10,
					p);
			break;
		case TOUCH_DOWN:
			// LEFT_UP
			path.reset();
			RectF oval1 = new RectF(0, 0, CENTER_LENGTH * 2, CENTER_LENGTH * 2);
			path.arcTo(oval1, -180 + DEGREE_PADDING, 90 - DEGREE_PADDING * 2);
			RectF oval = new RectF(//
					CENTER_LENGTH - INNER_RIDUS_STROKE, //
					CENTER_LENGTH - INNER_RIDUS_STROKE, //
					CENTER_LENGTH + INNER_RIDUS_STROKE, //
					CENTER_LENGTH + INNER_RIDUS_STROKE);
			path.arcTo(oval, -90 - DEGREE_PADDING, -90 + DEGREE_PADDING * 2);
			path.close();

			// UP_RIGHT
			// path.reset();
			path2.reset();
			path2.arcTo(oval1, -90 + DEGREE_PADDING, 90 - DEGREE_PADDING * 2);
			path2.arcTo(oval, 0 - DEGREE_PADDING, -90 + DEGREE_PADDING * 2);
			path2.close();

			// RIGHT_BOTTOM
			path3.reset();
			path3 = new Path();
			path3.arcTo(oval1, 0 + DEGREE_PADDING, 90 - DEGREE_PADDING * 2);
			path3.arcTo(oval, 90 - DEGREE_PADDING, -90 + DEGREE_PADDING * 2);
			path3.close();

			// BOTTOM_LEFT
			path4 = new Path();
			path4.reset();
			path4.arcTo(oval1, 90 + DEGREE_PADDING, 90 - DEGREE_PADDING * 2);
			path4.arcTo(oval, -180 - DEGREE_PADDING, -90 + DEGREE_PADDING * 2);
			path4.close();

			canvas.drawPath(path, p);
			canvas.drawPath(path2, p);
			canvas.drawPath(path3, p);
			canvas.drawPath(path4, p);
			break;
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setState(State.TOUCH_DOWN);
			if (timer != null)
				timer.cancel();
			timer = new Timer();
			timer.schedule(new TimerTask() {

				@Override
				public void run() {
					if (state == State.TOUCH_DOWN) {
						setState(State.LONG_PRESSED);
						handler.sendEmptyMessage(MSG_WHAT_INVAIDATE);
					}
				}
			}, 1000 * 2);

			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			if(state == State.LONG_PRESSED){
				return super.onTouchEvent(event);
			}
			break;
		case MotionEvent.ACTION_UP:
			if(state == State.TOUCH_DOWN){
				computePoint(event);
			}
			if (timer != null)
				timer.cancel();
			setState(State.FLOATING);
			invalidate();
			break;
		}
		return true;
	}

	/**
	 * compute which section you touched, and execute comman in this section.
	 * @param event
	 */
	private void computePoint(MotionEvent event) {
		
		
	}

	private void setState(State state) {
		L.v(TAG, "setState: " + state);
		this.state = state;
	}

}